using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Utilities
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TableReference 
    {
        #region props
        private string _ParentDatabase;
        /// <summary></summary>
        public string ParentDatabase
        {
            get
            {
                return this._ParentDatabase;
            }
            set
            {
                this._ParentDatabase = value;
            }
        }
        private string _ParentSchema;
        /// <summary></summary>
        public string ParentSchema
        {
            get
            {
                return this._ParentSchema;
            }
            set
            {
                this._ParentSchema = value;
            }
        }
        private string _ParentTableName;
        /// <summary></summary>
        public string ParentTableName
        {
            get
            {
                return this._ParentTableName;
            }
            set
            {
                this._ParentTableName = value;
            }
        }
        private string _ParentColumnName;
        /// <summary></summary>
        public string ParentColumnName
        {
            get
            {
                return this._ParentColumnName;
            }
            set
            {
                this._ParentColumnName = value;
            }
        }

        private string _ChildDatabase;
        /// <summary></summary>
        public string ChildDatabase
        {
            get
            {
                return this._ChildDatabase;
            }
            set
            {
                this._ChildDatabase = value;
            }
        }
        private string _ChildSchema;
        /// <summary></summary>
        public string ChildSchema
        {
            get
            {
                return this._ChildSchema;
            }
            set
            {
                this._ChildSchema = value;
            }
        }
        private string _ChildTableName;
        /// <summary></summary>
        public string ChildTableName
        {
            get
            {
                return this._ChildTableName;
            }
            set
            {
                this._ChildTableName = value;
            }
        }
        private string _ChildColumnName;
        /// <summary></summary>
        public string ChildColumnName
        {
            get
            {
                return this._ChildColumnName;
            }
            set
            {
                this._ChildColumnName = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pTbl"></param>
        /// <param name="pk"></param>
        /// <param name="fTbl"></param>
        /// <param name="fk"></param>
        public TableReference(string pTbl, string pk, string fTbl, string fk)
        {
            this._ParentDatabase = string.Empty;
            this._ParentSchema = "dbo";
            this._ParentTableName = pTbl;
            this._ParentColumnName = pk;
            this._ChildDatabase = string.Empty;
            this._ChildSchema = "dbo";
            this._ChildTableName = fTbl;
            this._ChildColumnName = fk;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pSchema"></param>
        /// <param name="pTable"></param>
        /// <param name="pk"></param>
        /// <param name="fSchema"></param>
        /// <param name="fTable"></param>
        /// <param name="fk"></param>
        public TableReference(string pSchema, string pTable, string pk, 
            string fSchema, string fTable, string fk) : this(pTable,pk,fTable,fk)
        {
            this._ParentSchema = pSchema;
            this._ChildSchema = fSchema;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="pSchema"></param>
        /// <param name="pTable"></param>
        /// <param name="pk"></param>
        /// <param name="fDb"></param>
        /// <param name="fSchema"></param>
        /// <param name="fTable"></param>
        /// <param name="fk"></param>
        public TableReference(string pDb, string pSchema, string pTable, string pk,
            string fDb, string fSchema, string fTable, string fk)
            : this(pSchema, pTable, pk, fSchema, fTable, fk)
        {
            this._ParentDatabase = pDb;
            this._ChildDatabase = fDb;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tblRef"></param>
        /// <returns></returns>
        public bool EqualsTo(TableReference tblRef)
        {
            if(this.ParentDatabase !=tblRef.ParentDatabase)
                return false;
            if (this.ParentSchema != tblRef.ParentSchema)
                return false;
            if (this.ParentTableName != tblRef.ParentTableName)
                return false;
            if (this.ParentColumnName != tblRef.ParentColumnName)
                return false;
            if (this.ChildDatabase != tblRef.ChildDatabase)
                return false;
            if (this.ChildSchema != tblRef.ChildSchema)
                return false;
            if (this.ChildTableName != tblRef.ChildTableName)
                return false;
            if (this.ChildColumnName != tblRef.ChildColumnName)
                return false;
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public TableReference ReverseReferenceDirection()
        {
            TableReference tblRef=new TableReference(
                this.ChildDatabase, this.ChildSchema, this.ChildTableName, this.ChildColumnName,
                this.ParentDatabase,this.ParentSchema,this.ParentTableName,this.ParentColumnName);
            return tblRef;
        }
    }
}
